package hot;

public class Num19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) return null;
        if(head.next == null) return null;
        ListNode node1 = head;
        ListNode node2 = head;
        ListNode dummyhead = new ListNode();
        dummyhead.next = head;
        ListNode node3 = dummyhead;
        for(int i = 0; i < n; i++) {
            node1 = node1.next;
        }
        while(node1 != null) {
            node1 = node1.next;
            node2 = node2.next;
            node3 = node3.next;
        }
        node3.next = node2.next;
        node2.next = null;
        return dummyhead.next;
    }
}
